package com.jbm.jbmsupplier.zp.utils;

import android.content.Context;
import android.util.Log;

import com.jbm.jbmsupplier.zp.entity.PullMessage;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by donar_work on 2017/4/24.
 */

/**
 * 保存小秘书数据的文件工具
 */

public class FileUtils {
    private static final String FILEPATH="receiveMsg.txt";
    private ObjectOutputStream out;
    private ObjectInputStream input;
    FileOutputStream fileOut;
    private Context context;
    public FileUtils(Context context){
    this.context=context;
    }

    public void Write(PullMessage message){
        try {
            fileOut = context.openFileOutput(FILEPATH,0);
            out=new ObjectOutputStream(fileOut);
            out.writeObject(message);
            out.flush();
          //  readObj();
            Log.e("main","保存成功");
        } catch (IOException e) {
            Log.e("main","保存失败"+e.getMessage());
            e.printStackTrace();
        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    public PullMessage readObj() {
        PullMessage obj = null;
        try {
            FileInputStream fileInput = context.openFileInput(FILEPATH);
            input=new ObjectInputStream(fileInput);
             obj = (PullMessage) input.readObject();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
return obj;
    }



}
